package com.zhentao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.TbLogin;
import com.zhentao.service.TbLoginService;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private TbLoginService tbLoginService;



    //--------------------管理端登录---------------------------
    @RequestMapping("login")
    public Result login(@RequestBody TbLogin tbLogin){
        QueryWrapper<TbLogin> wrapper= new QueryWrapper();
        wrapper.eq(tbLogin.getUsername()!=null,"username",tbLogin.getUsername());
        wrapper.eq(tbLogin.getPassword()!=null,"password",tbLogin.getPassword());
        TbLogin login = tbLoginService.getOne(wrapper);
        return login!=null?Result.OK(login):Result.ERROR("请输入正确的登录信息");
    }


    //系统下的----------------------------------操作日志--------------分页列表
    @RequestMapping("findAll")
    public Result findAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size){
        Page<TbLogin> tbLoginPage = new Page<>(page,size);
        Page<TbLogin> result = tbLoginService.page(tbLoginPage);
        return Result.OK(result);
    }


    //系统下的----------------------------------操作日志---------------条件查询
    @RequestMapping("search")
    public Result search(@RequestBody TbLogin tbLogin){
            QueryWrapper<TbLogin> wrapper = new QueryWrapper<>();
            wrapper.eq(tbLogin.getUsername()!=null, "username",tbLogin.getUsername());
//        // 添加create_time区间查询条件 - 查询当前日期的下个月之前的数据
//            LocalDateTime nextMonth = LocalDateTime.now().plusMonths(1);
//            wrapper.lt("create_time", nextMonth);

            TbLogin search = tbLoginService.getOne(wrapper);
            return search!=null?Result.OK(search):Result.ERROR("查询数据不存在");
        }
    }





